package TripleTable;
import SeqList.SeqList;
import TripleList.TripleList;
import Stack.Stack;

public class TripleTable<T> implements ITripleTable<T>{
    int len, size;
    public TripleList data;

    public TripleTable(int size){
        data = new TripleList(size);
        for(int i = 0;i < size;i++){
            data.insert(i,new ThreeTuple());
        }
        this.size = size - 1;
        this.len = 0;
    }

    @Override
    public boolean isEmpty() {
        return this.len == 0;
    }

    @Override
    public int getWeight(int row, int col) {
        for(int i = 0;i <= this.len;i++){
            if(this.data.getRow(i) == row && this.data.getCol(i) == col){
                return this.data.getWeight(i);
            }
        }
        return -1;
    }

    @Override
    public int getRow(int i) {
        return this.data.getRow(i);
    }

    @Override
    public int getCol(int i) {
        return this.data.getCol(i);
    }

    @Override
    public int getWeight(int i) {
        return this.data.getWeight(i);
    }

    @Override
    public void set(int row, int col, int weight) {//可以用insert()方法替代
        for(int i = 0;i <= this.len;i++){
            if(this.data.getRow(i) == row && this.data.getCol(i) == col){
                this.data.setWeight(i, weight);
            }
            if(this.data.getRow(i) == col && this.data.getCol(i) == row){
                this.data.setWeight(i, weight);
            }
        }
    }

    @Override
    public int length() {//求景点个数
        return this.len;
    }

    @Override
    public void insert(int row, int col, int weight) {
        int flag = 0;
        for(int i = 0;i <= this.len;i++){//假如增加的路径为已有路径
            if(this.data.getRow(i) == row && this.data.getCol(i) == col){
                this.data.setWeight(i, weight);
                flag = 1;
            }
            if(this.data.getRow(i) == col && this.data.getCol(i) == row){
                this.data.setWeight(i, weight);
                flag = 1;
            }
        }
        if (flag == 1){
            return;
        }
        if (this.len == this.size){//满串扩容
            TripleList temp = this.data;
            this.data = new TripleList(size + 1);
            for(int i = 0;i < size; i++){
                this.data.set(i, temp.get(i));
            }
            this.size++;
        }
        data.setRow(this.len, row);
        data.setCol(this.len, col);
        data.setWeight(this.len, weight);
        this.len++;
    }

    @Override
    public void remove(int row, int col) {
        int flag = 0;
        for(int i = 0;i <= this.len;i++){
            if(this.data.getRow(i) == row && this.data.getCol(i) == col){
                for(int j = i; j < this.len - 1; j++){//把i后的数据元素依次前移
                    this.data.set(i, this.data.get(j));
                }
                this.len--;
                flag = 1;
            }
            if(this.data.getRow(i) == col && this.data.getCol(i) == row){
                for(int j = i; j < this.len - 1; j++){//同上
                    this.data.set(i, this.data.get(j));
                }
                this.len--;
            }
        }
        if(flag == 0){
            System.out.println("it's out of index.");
        }
    }

    @Override
    public void removeAll() {
        this.len = 0;
    }

    @Override
    public TripleList getData() {
        TripleList data = this.data;
        return data;
    }
}
